<template>
 <!-- 故障列表 -->
  <div>
    <div class="table-page-search-wrapper">
      <!-- 查询条件 -->
      <a-form layout="inline" class="formModel" style="padding:20px 20px 0 20px">
        <a-row :gutter="48">
          <a-col :md="6" :sm="24">
            <a-form-item label="时间">
              <a-range-picker v-model="timeSelect" :ranges="{ Today: [moment(), moment()], 'This Month': [moment(), moment().endOf('month')] }" @change="onChange" />
            </a-form-item>
          </a-col>
          <a-col :md="1" :sm="24">
            <a-button type="primary" @click="selectEvent">查询</a-button>
          </a-col>
          <a-col :md="2" :sm="24">
            <a-button @click="download" type="primary" icon="export">excel导出</a-button>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 表格 -->
    <a-table :columns="columns" :dataSource="dataSource" :pagination="false" :loading="tableLoading" 
         bordered size="middle" style="width:100%;padding:5px 15px">
    </a-table>
  </div>
</template>

<script>
import { getProblemFirst ,exportExcelFirst} from '@/api/public'
import moment from 'moment'
export default {
  components: {},
  data() {
    return {
      dataSource: [],
      tableLoading: false,
      columns,
      timeSelect:'',
      queryParam: {},
    }
  },
  created() {
    this.timeSelect = [moment(),moment()]
    this.queryParam.startTime = moment().startOf('day').format("YYYY-MM-DD HH:mm:ss")
    this.queryParam.endTime =moment().endOf('day').format("YYYY-MM-DD HH:mm:ss")
    this.getDataInfo()
     var that = this
    document.onkeydown = function (e) { 
      let e1 = e || event || window.event || arguments.callee.caller.arguments[0]
      if (e1 && e1.keyCode == 13) {
        that.selectEvent() 
      } 
    }
  },
  methods: {
    moment,
    //键盘监听
    handleKeyDown(e) {
      let key = null
      if (window.event === undefined) {
        key = e.keyCode
      } else {
        key = window.event.keyCode
      }
      if (key === 13) {
        //触发的事件
        this.selectEvent()
      }
    },
    //表格数据查询
    getDataInfo() {
      this.tableLoading = true
      getProblemFirst(this.queryParam.startTime,this.queryParam.endTime).then(
        (res) => {
          res.code == 200 ? (this.dataSource = res.data) : this.$message.error(res.msg)
          this.tableLoading = false
        },
        (error) => {
          this.$message.error(error)
        }
      )
    },
    //文档导出
    download() {
      exportExcelFirst(this.queryParam.startTime,this.queryParam.endTime)
        .then((response) => {
          let blob = new Blob([response], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' })
          let url = window.URL.createObjectURL(blob)
          let aLink = document.createElement('a')
          aLink.style.display = 'none'
          aLink.href = url
          aLink.setAttribute('download', '故障列表.xlsx')
          document.body.appendChild(aLink)
          aLink.click()
          document.body.removeChild(aLink)
          window.URL.revokeObjectURL(url)
          this.$message.success(`文件已导出`)
        })
        .catch((error) => {})
    },
    //时间查询
    onChange(date, dateString) {
      this.queryParam.startTime = dateString[0] + ' 00:00:00'
      this.queryParam.endTime = dateString[1] + ' 23:59:59'
    },
    //查询
    selectEvent() {
      this.getDataInfo()
    },
  },
}

const columns = [
  {
    title: '区县',
    dataIndex: 'area',
    width: 60,
    align: 'center',
  },
  {
    title: '及时率',
    align: 'center',
    children: [
      {
        title: '故障处理及时率',
        dataIndex: 'specialLine',
        width: 100,
        align: 'center',
      },
      {
        title: '政企投诉处理及时率',
        dataIndex: 'zqComplain',
        width: 100,
        align: 'center',
      },
    ],
  },
  {
    title: '满意度',
    align: 'center',
    children: [
      {
        title: '政企投诉满意度',
        dataIndex: 'zqSatisfaction',
        width: 100,
        align: 'center',
      },
    ],
  },
]
</script>

<style >
</style>
